package com.example.stock.service.impl;

import com.example.stock.controller.dto.DailyDetailDTO;
import com.example.stock.mapper.DailyDetailMapper;
import com.example.stock.mapper.DrawDownSatMapper;
import com.example.stock.model.DailyDetail;
import com.example.stock.model.DailySat;
import com.example.stock.model.DrawDownSat;
import com.example.stock.model.DrawDownYearSat;
import com.example.stock.service.DailyDataService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DailyDataServiceImpl implements DailyDataService {

    @Autowired
    DailyDetailMapper dailyDetailMapper;

    @Autowired
    DrawDownSatMapper drawDownSatMapper;

    Map<String, List<DailyDetailDTO>> dailyDetailMap = new HashMap<>();

    @Override
    public List<DailyDetailDTO> getDailyData(String stock) {
        if (dailyDetailMap.containsKey(stock)) {
            return dailyDetailMap.get(stock);
        }

        List<DailyDetail> dailyDetailByStock = dailyDetailMapper.getDailyDetailByStock(stock);
        List<DailyDetailDTO> dailyDetailDTOList = new ArrayList<>();

        // 取后2000个数据
        dailyDetailByStock.subList(Math.max(0, dailyDetailByStock.size() - 2000), dailyDetailByStock.size()).forEach(i -> {

            DailyDetailDTO dailyDetailDTO = new DailyDetailDTO();
            BeanUtils.copyProperties(i, dailyDetailDTO);

            dailyDetailDTOList.add(dailyDetailDTO);
        });
        dailyDetailMap.put(stock, dailyDetailDTOList);
        return dailyDetailDTOList;
    }

    @Override
    public Map<String, List<DailyDetailDTO>> getDailyDataByStocks(List<String> stocks) {
        Map<String, List<DailyDetailDTO>> dailyDetailMap = new HashMap<>();
        stocks.forEach(stock -> {
            dailyDetailMap.put(stock, getDailyData(stock));
        });

        return dailyDetailMap;
    }

    @Override
    @Cacheable(value = "getDailyDataByDate", key = "#date")
    public List<DailyDetail> getDailyDataByDate(String date) {
        return dailyDetailMapper.getDailyDetailByDate(date);
    }

    @Override
    public List<DailyDetail> getLastedDailyDataByDate() {
        return dailyDetailMapper.getDailyDetailByDate(getDates().getFirst());
    }

    @Override
    public List<String> getDates() {
        return dailyDetailMapper.getDates();
    }

    @Override
    public DailyDetailDTO getLastedDailyDetailByStock(String stock) {
        return getDailyData(stock).getLast();
    }

    Map<String, Map<String,Double>> dailyCloseMap = new HashMap<>();

    @Override
    public Double getDailyClose(String stock, String date) {
        if (dailyCloseMap.containsKey(stock)) {
            return dailyCloseMap.get(stock).get(date);
        }
        List<DailyDetailDTO> dailyData = getDailyData(stock);
        Map<String, Double> collect = dailyData.stream()
                .collect(Collectors.toMap(DailyDetailDTO::getDate, DailyDetailDTO::getClose));
        dailyCloseMap.put(stock, collect);
        return collect.get(date);
    }

    @Override
    public List<DrawDownSat> getDrawDownSatData() {
        return drawDownSatMapper.getDrawDownSatData();
    }

    @Override
    public Map<String, DrawDownYearSat> getDrawDownYearSatByStock(String stock) {
        List<DrawDownYearSat> drawDownYearSatByStock = drawDownSatMapper.getDrawDownYearSatByStock(stock);

        return drawDownYearSatByStock.stream().collect(Collectors.toMap(DrawDownYearSat::getYear, i -> i));
    }

    @Override
    public List<DailySat> getDailyStaByYear(String year) {
        return dailyDetailMapper.getDailyStaByYear(year);
    }
}